import numpy as np
# f = w * x
# f = 2 * x, we set w as 2
x = np.array([1, 2, 3, 4, 5, 6], dtype=np.float32)
y = np.array([2, 4, 6, 8, 10, 12], dtype=np.float32)
# init weight
w = 0.0
# model prediction
# 這邊之後會解釋為啥叫做 forward,可以先視為計算函數而已
def forward(x):
return w * x
# set up loss function as mean square error
def loss(y, y_predicted):
return ((y_predicted-y) ** 2).mean()
# gradient
def gradient(x, y, y_predicted):
return np.dot(2*x, y_predicted-y).mean()
print(f'Prediction before training: f(5) = {forward(5): .3f}')
# Training
learning_rate = 0.01
n_iters = 10
for epoch in range(n_iters):
# perdiction = forward pass
y_pred = forward(x)
# loss
l = loss(y, y_pred)
# we know that gradient descent is where
# calculate gradient and update parameters
# calculation of gradients
dw = gradient(x, y, y_pred)
# update weights
w -= learning_rate * dw
if epoch % 1 == 0:
print(f'epoch {epoch + 1}: w = {w:.3f}, loss = {l:.8f}')
print(f'Prediction after training: f(5) = {forward(5): .3f}')